from   myapp            import app , myapp
from   myapp_tests_base import MyAppTestCase
import unittest

exec(open(app.root_path + '/constants/tests_constants.py').read())


class ResourcesTestCase(MyAppTestCase):

  def test_index_no_posts(self):
    with app.app_context(): myapp.init_db()

    resp = self.client.get(INDEX_POSTS_ROUTE)
    self.assertEqual(resp.status_code , SUCCESS_STATUS)
    self.assertIn   (INDEX_POSTS_TEXT , resp.data     )
    self.assertIn   (NO_POSTS_TEXT    , resp.data     )


  def test_index_posts(self):
    resp = self.client.get(INDEX_POSTS_ROUTE)
    self.assertEqual(resp.status_code , SUCCESS_STATUS)
    self.assertIn   (INDEX_POSTS_TEXT , resp.data     )
    self.assertNotIn(NO_POSTS_TEXT    , resp.data     )


  def test_show_unknown_post_returns_404(self):
    resp = self.client.get(INDEX_POSTS_ROUTE + '/unknown-resource')
    self.assertEqual(resp.status_code , NOTFOUND_STATUS)


  def test_show_known_post(self):
    resp = self.client.get(TEST_POST_1_PATH)
    self.assertEqual(resp.status_code                  , SUCCESS_STATUS)
    self.assertIn   (SHOW_POST_TEXT + b'1'             , resp.data     )
    self.assertIn   (TEST_POST_1[TITLE_KEY  ].encode() , resp.data     )
    self.assertIn   (TEST_POST_1[MESSAGE_KEY].encode() , resp.data     )


  def test_new_post_redirects_visitors_to_login(self):
    resp = self.client.get(NEW_POST_ROUTE)
    self.assertEqual(resp.status_code           , REDIRECT_STATUS        )
    self.assertRegex(resp.headers[RESP_URL_KEY] , r'' + LOGIN_ROUTE + '$')


  def test_new_post_renders_for_members(self):
    self.login(ADMIN_LOGIN , ADMIN_PASS)

    resp = self.client.get(NEW_POST_ROUTE)
    self.assertEqual(resp.status_code , SUCCESS_STATUS)
    self.assertIn   (NEW_POST_TEXT    , resp.data     )


  def test_visitors_can_not_create_posts(self):
    resp = self.client.post(CREATE_POST_ROUTE , data = CREATE_POST_PARAMS)
    self.assertEqual(resp.status_code  , UNAUTH_STATUS)
    self.assertIn   (UNAUTHORIZED_TEXT , resp.data    )


  def test_members_can_create_posts(self):
    self.login(ADMIN_LOGIN , ADMIN_PASS)

    resp = self.client.post(CREATE_POST_ROUTE , data = CREATE_POST_PARAMS , follow_redirects = True)
    self.assertEqual(resp.status_code                , SUCCESS_STATUS)
    self.assertNotIn(NO_POSTS_TEXT                   , resp.data     )
    self.assertIn   (SHOW_POST_TEXT + b'3'           , resp.data     )
    self.assertIn   (TITLE_ESCAPED_TEXT              , resp.data     )
    self.assertIn   (CREATE_POST_PARAMS[MESSAGE_KEY] , resp.data     )

    resp = self.client.get(INDEX_POSTS_ROUTE)
    self.assertEqual(resp.status_code                  , SUCCESS_STATUS)
    self.assertIn   (TEST_POST_1[TITLE_KEY  ].encode() , resp.data     )
    self.assertIn   (TEST_POST_1[MESSAGE_KEY].encode() , resp.data     )
    self.assertIn   (TEST_POST_2[TITLE_KEY  ].encode() , resp.data     )
    self.assertIn   (TEST_POST_2[MESSAGE_KEY].encode() , resp.data     )
    self.assertIn   (TITLE_ESCAPED_TEXT                , resp.data     )
    self.assertIn   (CREATE_POST_PARAMS[MESSAGE_KEY]   , resp.data     )


  def test_create_post_redirects_members_to_show_page(self):
    self.login(ADMIN_LOGIN , ADMIN_PASS)

    resp = self.client.post(CREATE_POST_ROUTE , data = CREATE_POST_PARAMS)
    self.assertEqual(resp.status_code           , REDIRECT_STATUS          )
    self.assertRegex(resp.headers[RESP_URL_KEY] , r'' + CREATED_POST_PATH + '$')


  def test_edit_post_redirects_visitors_to_login(self):
    resp = self.client.get(TEST_POST_EDIT_PATH)
    self.assertEqual(resp.status_code           , REDIRECT_STATUS        )
    self.assertRegex(resp.headers[RESP_URL_KEY] , r'' + LOGIN_ROUTE + '$')


  def test_edit_post_renders_for_members(self):
    self.login(ADMIN_LOGIN , ADMIN_PASS)

    resp = self.client.get(TEST_POST_EDIT_PATH)
    self.assertEqual(resp.status_code                  , SUCCESS_STATUS)
    self.assertIn   (EDIT_POST_TEXT + b'2'             , resp.data     )
    self.assertIn   (TEST_POST_2[TITLE_KEY  ].encode() , resp.data     )
    self.assertIn   (TEST_POST_2[MESSAGE_KEY].encode() , resp.data     )


  def test_visitors_can_not_update_posts(self):
    resp = self.client.post(TEST_POST_UPDATE_PATH , data = UPDATE_POST_PARAMS)
    self.assertEqual(resp.status_code  , UNAUTH_STATUS)
    self.assertIn   (UNAUTHORIZED_TEXT , resp.data    )


  def test_members_can_update_posts(self):
    self.login(ADMIN_LOGIN , ADMIN_PASS)

    resp = self.client.post(TEST_POST_UPDATE_PATH , data = UPDATE_POST_PARAMS , follow_redirects = True)
    self.assertEqual(resp.status_code                         , SUCCESS_STATUS)
    self.assertIn   (UPDATED_POST_TEXT                        , resp.data     )
    self.assertNotIn(TEST_POST_2       [TITLE_KEY  ].encode() , resp.data     )
    self.assertNotIn(TEST_POST_2       [MESSAGE_KEY].encode() , resp.data     )
    self.assertIn   (TITLE_ESCAPED_TEXT                       , resp.data     )
    self.assertIn   (UPDATE_POST_PARAMS[MESSAGE_KEY]          , resp.data     )


  def test_update_post_redirects_members_to_show_page(self):
    self.login(ADMIN_LOGIN , ADMIN_PASS)

    resp = self.client.post(TEST_POST_UPDATE_PATH , data = UPDATE_POST_PARAMS)
    self.assertEqual(resp.status_code           , REDIRECT_STATUS              )
    self.assertRegex(resp.headers[RESP_URL_KEY] , r'' + CREATED_POST_PATH + '$')


  def test_visitors_can_not_destroy_posts(self):
    resp = self.client.delete(TEST_POST_DELETE_PATH)
    self.assertEqual(resp.status_code  , UNAUTH_STATUS)
    self.assertIn   (UNAUTHORIZED_TEXT , resp.data    )


  def test_members_can_destroy_posts(self):
    self.login(ADMIN_LOGIN , ADMIN_PASS)

    resp = self.client.delete(TEST_POST_DELETE_PATH , data = UPDATE_POST_PARAMS , follow_redirects = True)
    self.assertEqual(resp.status_code  , SUCCESS_STATUS)
    self.assertIn   (DELETED_POST_TEXT , resp.data     )


  def test_destroy_post_redirects_members_to_index_page(self):
    self.login(ADMIN_LOGIN , ADMIN_PASS)

    resp = self.client.delete(TEST_POST_DELETE_PATH , data = UPDATE_POST_PARAMS)
    self.assertEqual(resp.status_code           , REDIRECT_STATUS              )
    self.assertRegex(resp.headers[RESP_URL_KEY] , r'' + INDEX_POSTS_ROUTE + '$')


if __name__ == '__main__':
  unittest.main()
